<template>
  <div>
    <h1 style="color: orange;">稿件管理页面</h1>
    <el-radio-group v-model="type" @input="loadContents()">
      <el-radio-button label="1">烘焙食谱</el-radio-button>
      <el-radio-button label="5">烘焙视频</el-radio-button>
      <el-radio-button label="8">行业资讯</el-radio-button>
    </el-radio-group>
    <el-button type="success" @click="post()">发布内容</el-button>
    <el-table :data="contentArr">
      <el-table-column align="center" type="index" label="商品编号" width="100px"/>
      <el-table-column align="center" prop="title" label="商品标题" width="100px"/>
      <el-table-column  v-if="type!=5" align="center" prop="brief" label="商品摘要" width="300px"/>
      <el-table-column align="center" prop="categoryName" label="商品类型" width="100px"/>
      <el-table-column align="center"  label="商品封面" width="100px">
        <template slot-scope="scope">
          <img :src="BASE_URL+scope.row.imgUrl" width="80px"/>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="viewCount" label="浏览量" width="100px"/>
      <el-table-column align="center" prop="commentCount" label="评论量" width="100px"/>
      <el-table-column align="center" prop="createTime" label="发布时间" width="100px"/>

      <el-table-column
          align="center"
          fixed="right"
          label="操作"
          width="200">
        <template slot-scope="scope" >
          <el-button @click="handleEditor(scope.$index,scope.row)" type="success" size="small">修改</el-button>
          <el-button @click="handleDelete(scope.$index,scope.row)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      type: 1,
      contentArr: [],
      user:localStorage.user ? JSON.parse(localStorage.user):null,
    }
  },
  methods: {
    post() {
      this.$router.push('/personal/post')
    },
    //加载内容
    loadContents(){
      let url = this.BASE_URL + '/v1/contents/selectAll/'+this.user.id+'/'+this.type
      this.axios.get(url).then(res=>{
        if(res.data.code == 1){
          //console.log(res)
          this.contentArr = res.data.data
          //this.$message.success("成功过")
        }else{
          this.$message.error(res.data.msg)
        }
      })
    },
    //删除内容
    handleDelete(index,row){
      //console.log(index,row)
      let url = this.BASE_URL + '/v1/contents/delete/'+row.id
      let data = row.title
      this.$confirm("你确认删除"+data+"标题吗？", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //console.log(this)
        this.axios.delete(url).then(res=>{
          if(res.data.code == 1){

            this.contentArr = this.contentArr.slice(index,1)
            location.reload()
            this.$message.success("删除成功")
          }else{
            this.$message.error(res.data.msg)
          }
        })

      }).catch(()=>{
        this.$message.success("取消成功")
      })
    },
    //修改功能
    handleEditor(index,row){
      this.$router.push('/personal/post?id='+row.id)
    }



  },
  mounted() {
    //加载完页面就获取页面内容
    this.loadContents()
  }
}
</script>

<style scoped>

</style>
